﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Proggy.Web.Cms.Core.Seo.Service.Abstract;
using Proggy.Web.Cms.Core.Seo.Service.Concrete;
using Proggy.Web.Cms.Seo.Service.Abstract;
using Proggy.Web.Cms.Seo.Service.Concrete;

namespace Proggy.Web.Cms.Seo.Tests
{
    [TestClass]
    public class Test_url_optimisation_service
    {
        /// <summary>
        /// Optimization service
        /// </summary>
        IUrlOptimizationService _serviceAddSlashNoWWW = new UrlOptimizationService(false, true);
        IUrlOptimizationService _serviceNoSlashNoWWW = new UrlOptimizationService(false, false);
        IUrlOptimizationService _serviceNoSlashAddWWW = new UrlOptimizationService(true,false);
        IUrlOptimizationService _serviceAddSlashAddWWW = new UrlOptimizationService(true, true);

        [TestMethod]
        public void Test_url_with_extension_dont_add_slash() { 
            // input
//            var input = new Uri("http://mysite.com/images/rob.is.shit.aspx?dave=true&x=1.png");
            var input = new Uri("http://mysite.com/images/rob.is.shit?dave=true&x=1.png");

            // expected
            var expected = input.OriginalString;

            // actual
            var actual = _serviceAddSlashNoWWW.IsOptimizedOkAlready(input).Item2.OriginalString;

            // assert
            Assert.AreEqual(expected, actual);
            
        }

        [TestMethod]
        public void Test_url_is_path_lowercase_false()
        {
            // input
            var input = new Uri("http://rob.DAVE.com/NEWS/1/abC?x=yyis");

            // outpus
            var expected = false;
            var actual = _serviceAddSlashNoWWW.PathIsLowercase(input);

            // test
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void Test_url_is_domain_lowercase_true()
        {
            // input
            var input = new Uri("http://rob.DAVE.com/news/1/abc?x=yyis");

            // outpus
            var expected = true;
            var actual = _serviceAddSlashNoWWW.PathIsLowercase(input);

            // test
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void Test_url_is_protocol_lowercase_true()
        {
            // input
            var input = new Uri("HTTP://rob.dave.com/news/1/abc?x=yyis");

            // outpus
            var expected = true;
            var actual = _serviceAddSlashNoWWW.PathIsLowercase(input);

            // test
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void Test_add_www()
        {
            // input
            var input = new Uri("http://google.co.uk/test?h=xyz");
            var expected = "www.google.co.uk";
            var actula = this._serviceAddSlashNoWWW.GetCorrectHost(input);
            Assert.AreEqual(expected, actula);

        }

        [TestMethod]
        public void Test_remove_www()
        {
            // input
            var input = new Uri("http://www.google.co.uk/test?h=xyz");
            var expected = "google.co.uk";
            var actula = this._serviceAddSlashNoWWW.GetCorrectHost(input);
            Assert.AreEqual(expected, actula);

        }

        [TestMethod]
        public void Test_www_or_not_is_correct()
        {
            // input
            var input1 = new Uri("http://google.co.uk/test?h=xyz");
            var expected1 = "google.co.uk";
            var actula1 = this._serviceAddSlashNoWWW.GetCorrectHost(input1);
            Assert.AreEqual(expected1, actula1);

            // input
            var input2 = new Uri("http://www.google.co.uk/test?h=xyz");
            var expected2 = "www.google.co.uk";
            var actula2 = this._serviceAddSlashNoWWW.GetCorrectHost(input2);
            Assert.AreEqual(expected2, actula2);

        }

        [TestMethod]
        public void Test_remove_trailing_slash()
        {
            //
            var input = new Uri("http://google.co.uk/test/");
            var expected = "/test";
            var actual = this._serviceAddSlashNoWWW.GetCorrectPath(input);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void Test_add_trailing_slash()
        {
            //
            var input = new Uri("http://google.co.uk/test");
            var expected = "/test/";
            var actual = this._serviceAddSlashNoWWW.GetCorrectPath(input);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void Test_keep_trailing_slash()
        {
            //
            var input = new Uri("http://google.co.uk/test/");
            var expected = "/test/";
            var actual = this._serviceAddSlashNoWWW.GetCorrectPath(input);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void Test_keep_no_trailing_slash()
        {
            //
            var input = new Uri("http://google.co.uk/test");
            var expected = "/test";
            var actual = this._serviceAddSlashNoWWW.GetCorrectPath(input);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void Test_lowercase_the_path()
        {
            //
            var input = new Uri("http://google.CO.UK/TESTsisiisisisisi");
            var expected = "/testsisiisisisisi";
            var actual = this._serviceAddSlashNoWWW.GetCorrectPath(input);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void Test_leave_the_path_alone()
        {
            //
            var input = new Uri("http://google.CO.UK/testsisiisisisisi");
            var expected = "/testsisiisisisisi";
            var actual = this._serviceAddSlashNoWWW.GetCorrectPath(input);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void Test_the_main_method_uppercase_issue() { 
            // 
            var input = new Uri("http://proggy.com/TEST/this/should/redirect/yaknow");
            var expected = new Tuple<bool, Uri>(false, new Uri("http://proggy.com/test/this/should/redirect/yaknow"));
            var actual = this._serviceAddSlashNoWWW.IsOptimizedOkAlready(input);
            Assert.AreEqual(expected.Item1, actual.Item1);
            Assert.AreEqual(expected.Item2, actual.Item2);

        }

        [TestMethod]
        public void Test_the_main_method_slash_issue()
        {
            // 
            var input = new Uri("http://proggy.com/TEST/this/should/redirect/yaknow/");
            var expected = new Tuple<bool, Uri>(false, new Uri("http://proggy.com/test/this/should/redirect/yaknow"));
            var actual = this._serviceAddSlashNoWWW.IsOptimizedOkAlready(input);
            Assert.AreEqual(expected.Item1, actual.Item1);
            Assert.AreEqual(expected.Item2, actual.Item2);

        }

        [TestMethod]
        public void Test_the_main_method_www_issue()
        {
            // 
            var input = new Uri("http://www.proggy.com/TEST/this/should/redirect/yaknow/");
            var expected = new Tuple<bool, Uri>(false, new Uri("http://proggy.com/test/this/should/redirect/yaknow"));
            var actual = this._serviceAddSlashNoWWW.IsOptimizedOkAlready(input);
            Assert.AreEqual(expected.Item1, actual.Item1);
            Assert.AreEqual(expected.Item2, actual.Item2);

        }

        [TestMethod]
        public void Test_the_main_method_uppercase_fine()
        {
            // 
            var input = new Uri("http://proggy.com/test/this/should/redirect/yaknow");
            var expected = new Tuple<bool, Uri>(true, new Uri("http://proggy.com/test/this/should/redirect/yaknow"));
            var actual = this._serviceAddSlashNoWWW.IsOptimizedOkAlready(input);
            Assert.AreEqual(expected.Item1, actual.Item1);
            Assert.AreEqual(expected.Item2, actual.Item2);

        }

        [TestMethod]
        public void Test_the_main_method_slash_fine()
        {
            // 
            var input = new Uri("http://proggy.com/test/this/should/redirect/yaknow/");
            var expected = new Tuple<bool, Uri>(true, new Uri("http://proggy.com/test/this/should/redirect/yaknow/"));
            var actual = this._serviceAddSlashNoWWW.IsOptimizedOkAlready(input);
            Assert.AreEqual(expected.Item1, actual.Item1);
            Assert.AreEqual(expected.Item2, actual.Item2);

        }

        [TestMethod]
        public void Test_the_main_method_www_fine()
        {
            // 
            var input = new Uri("http://www.proggy.com/test/this/should/redirect/yaknow");
            var expected = new Tuple<bool, Uri>(true, new Uri("http://www.proggy.com/test/this/should/redirect/yaknow"));
            var actual = this._serviceAddSlashNoWWW.IsOptimizedOkAlready(input);
            Assert.AreEqual(expected.Item1, actual.Item1);
            Assert.AreEqual(expected.Item2, actual.Item2);

        }
    }
}
